package Test;

import javax.imageio.ImageIO;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @leiwenkang 2018-01-29 18:09.
 */
public class MergeImage {
    public static BufferedImage getBufferedImage(String path)
            throws IOException {
        URL url = new URL(path); //声明url对象
        URLConnection connection = url.openConnection(); //打开连接
        connection.setDoOutput(true);
        return ImageIO.read(connection.getInputStream());
    }

    public static void overlapImage(BufferedImage big, BufferedImage small, String outFile) {
        try {
            Graphics2D g = big.createGraphics();
            float alpha = 0.6f;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            g.drawImage(small, 0, 0, small.getWidth(), small.getHeight(), null);
            g.dispose();
            ImageIO.write(big, outFile.split("\\.")[1], new File(outFile));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        String bigPath = "E:/GREEN.png";
        String smallPath = "E:/black.png";
        BufferedImage big = ImageIO.read(new File(bigPath));
        BufferedImage small = ImageIO.read(new File(smallPath));
        overlapImage(big, small, "E:/gre.jpg");
    }
}
